package com.th.controller;

import com.alibaba.fastjson.JSON;
import com.th.common.domain.UsInternalMessage;
import com.th.common.utils.SseEmitterUtil;
import com.th.service.SSEService;
import com.th.service.UsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author ErrorRua
 * @date 2023/06/25
 * @description:
 */
@RestController
@RequestMapping("/sse")
public class SSEController {

    @Autowired
    private SSEService sseService;

    @GetMapping("/connect")
    public SseEmitter connect(){
        return sseService.connect();
    }


    @DeleteMapping("/disconnect/{userId}")
    public void disconnect(@PathVariable Long userId){
        sseService.disconnect(userId);
    }

    @PostMapping("/test")
    public void test(@RequestBody UsInternalMessage usInternalMessage){
        SseEmitterUtil.batchSendMessage(JSON.toJSONString(usInternalMessage));
    }
}
